package com.fh.qqserver.service;

import com.fh.qqcommon.Message;
import com.fh.qqcommon.MessageType;
import com.fh.qqcommon.User;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class QQServer {
    private ServerSocket serverSocket = null;
    //创建一个集合，存放多个用户，如果是这些用户登录，就认为是合法
    private static HashMap<String,User> validUsers = new HashMap<>();
    static{
        //在静态代码块初始化 validUsers
        validUsers.put("100",new User("123456","100"));
        validUsers.put("200",new User("123456","200"));
        validUsers.put("300",new User("123456","300"));
        validUsers.put("派大星",new User("123456","派大星"));
        validUsers.put("海绵宝宝",new User("123456","海绵宝宝"));
        validUsers.put("章鱼哥",new User("123456","章鱼哥"));
    }
    //验证用户是否有效的方法
    private boolean checkUser(String userId,String password){
        User user = validUsers.get(userId);
        if(user==null){//说明userId没有存在
            return false;
        }
        if(!user.getPassword().equals(password)){//userId正确,但是密码错误
            return false;
        }
        return true;
    }
    public QQServer() {
        try {
            System.out.println("服务器在 9999 端口监听。。。");
            //启动推送新闻的线程
            new Thread(new SendNewsToAllService()).start();
            serverSocket = new ServerSocket(9999);
            try {
                while (true) { // 持续监听客户端连接
                    Socket socket = serverSocket.accept();
                    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    User u = (User) objectInputStream.readObject();
                    Message message = new Message();
                    //验证用户
                    if (checkUser(u.getUserID(),u.getPassword())) {
                        message.setMesType(MessageType.MESSAGE_LOGIN_SUCCESS);
                        objectOutputStream.writeObject(message);
                        ServerConnectClientThread serverConnectClientThread = new ServerConnectClientThread(socket, u.getUserID());
                        serverConnectClientThread.start();
                        ManageClientThreads.addClientThread(u.getUserID(), serverConnectClientThread);
                    } else {//登录失败
                        System.out.println("用户 id = "+u.getUserID()+ " pwd = "+u.getPassword() + " 验证失败" );
                        message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                        objectOutputStream.writeObject(message);
                        socket.close();
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}